package com.yun.zf.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yun.zf.admin.mapper.ZfPactCertMapper;
import com.yun.zf.admin.mapper.ZfPactPersonInfoMapper;
import com.yun.zf.admin.service.ZfPactCertService;
import com.yun.zf.admin.service.ZfPactPersonInfoService;
import com.yun.zf.common.admin.domain.ZfPactCert;
import com.yun.zf.common.admin.domain.ZfPactPersonInfo;
import com.yun.zf.common.admin.dto.pact.PactPersonDto;
import com.yun.zf.common.admin.model.customer.PactCertMo;
import com.yun.zf.common.admin.model.pact.PactPersonListMo;
import com.yun.zf.common.admin.model.pact.PactPersonMo;
import com.yun.zf.common.mapstruct.admin.PactMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 合同甲乙双方信息 服务实现类
 * </p>
 *
 * @author cj
 * @since 2021-07-24
 */
@Service
public class ZfPactPersonInfoServiceImpl extends ServiceImpl<ZfPactPersonInfoMapper, ZfPactPersonInfo> implements ZfPactPersonInfoService {

    @Autowired
    private ZfPactCertService certService;
    @Autowired
    private ZfPactCertMapper certMapper;

    @Override
    public List<PactPersonDto> getPactPersons(Long provCode, Long cityCode, Long pactId) {
        return baseMapper.getPersons(provCode, cityCode, pactId);
    }
    @Override
    public List<PactPersonDto> getPactPersonsDetail(Long provCode, Long cityCode, Long pactId) {
        List<PactPersonDto> personMos = baseMapper.getPersonsNotJ(provCode, cityCode, pactId);
        for (PactPersonDto p :personMos
             ) {
            List<PactCertMo> certMos=  certMapper.getCerts(Long.parseLong(p.getPersonId()),provCode,cityCode);
            p.setCerts(certMos);
        }
        return  personMos;
    }

    @Override
    public int delByPactId(Long provCode, Long cityCode, Long pactId) {
        QueryWrapper<ZfPactPersonInfo> personInfoQueryWrapper = new QueryWrapper<>();
        personInfoQueryWrapper.lambda().ne(ZfPactPersonInfo::getType,1).eq(ZfPactPersonInfo::getPactId,pactId).eq(ZfPactPersonInfo::getCityCode,cityCode).eq(ZfPactPersonInfo::getProvinceCode,provCode);
        return baseMapper.delete(personInfoQueryWrapper);
    }

    @Override
    @Transactional
    public void addPactPerson(PactPersonListMo persons, Long provCode, Long cityCode) {
        // 添加之前先删除除甲方产权人信息之外的信息
        this.delByPactId(provCode,cityCode,Long.parseLong(persons.getPactId()));
        // 删除图片信息
        certService.delCert(Long.parseLong(persons.getPactId()),provCode,cityCode);

        if (persons.getPersonMos()!=null&& persons.getPersonMos().size()>0){
            for (PactPersonMo p: persons.getPersonMos()
            ) {
                List<ZfPactCert> certList = new ArrayList<>();
                ZfPactPersonInfo  personInfo = PactMapper.INSTANCE.moToPersonList(p);
                personInfo.setCityCode(cityCode);
                personInfo.setProvinceCode(provCode);
                personInfo.setPactId(Long.parseLong(persons.getPactId()));
                baseMapper.insert(personInfo);
                List<PactCertMo> certs = p.getCerts();
                if (certs!=null&&certs.size()>0){
                    for (PactCertMo c: certs
                    ) {
                        ZfPactCert cert = new ZfPactCert();
                        cert.setCityCode(cityCode);
                        cert.setProvinceCode(provCode);
                        cert.setPath(c.getPath());
                        cert.setUrl(c.getUrl());
                        cert.setPactId(Long.parseLong(persons.getPactId()));
                        cert.setPersonId(personInfo.getId());
                        certList.add(cert);
                    }
                    certService.saveBatch(certList);
                }

            }
        }


    }
}
